package com.flyco.modules.returns.service.impl;

import com.alibaba.fastjson.JSON;
import com.flyco.common.util.IdGenerator16Bit;
import com.flyco.modules.returns.dto.BatchNoVO;
import com.flyco.modules.returns.entity.OmsOrderReturnApplyOms;
import com.flyco.modules.returns.entity.OmsOrderReturnItem;
import com.flyco.modules.returns.entity.OmsOrderReturnItemBatch;
import com.flyco.modules.returns.entity.OmsOrderReturnItemOms;
import com.flyco.modules.returns.mapper.OmsOrderReturnItemOmsMapper;
import com.flyco.modules.returns.service.IOmsOrderReturnApplyOmsService;
import com.flyco.modules.returns.service.IOmsOrderReturnItemOmsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 退货申请明细中间表
 * @Author: flyco
 * @Date: 2023-08-25
 * @Version: V1.0
 */
@Service
public class OmsOrderReturnItemOmsServiceImpl extends ServiceImpl<OmsOrderReturnItemOmsMapper, OmsOrderReturnItemOms> implements IOmsOrderReturnItemOmsService {

    @Autowired
    private OmsOrderReturnItemOmsMapper returnItemOmsMapper;
    @Autowired
    private IOmsOrderReturnApplyOmsService returnApplyOmsService;

    @Override
    public void saveUseLessByItems(List<OmsOrderReturnItemBatch> itemList, Long returnApplyOmsId) {

        saveByItems(itemList, returnApplyOmsId, "useLess");
    }

    @Override
    public void saveReUseByItems(List<OmsOrderReturnItemBatch> itemList, Long returnApplyOmsId) {

        saveByItems(itemList, returnApplyOmsId, "reUser");
    }


    private void saveByItems(List<OmsOrderReturnItemBatch> itemList, Long returnApplyOmsId, String type) {

        List<OmsOrderReturnItemOms> returnItemOmsList = new ArrayList<>();

        for (OmsOrderReturnItemBatch returnItem : itemList) {
            saveByItem(returnItem, returnApplyOmsId, type, returnItemOmsList);
        }
        saveBatch(returnItemOmsList);

        //根据拆分后的详情，反写拆分后的申请中的total_amount
        BigDecimal totalAmount = returnItemOmsList.stream().map(OmsOrderReturnItemOms::getTotalAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        OmsOrderReturnApplyOms returnApplyOms = returnApplyOmsService.getById(returnApplyOmsId);
        returnApplyOms.setReturnAmount(totalAmount);
        returnApplyOmsService.updateById(returnApplyOms);
    }

    private void saveByItem(OmsOrderReturnItemBatch item, Long returnApplyOmsId, String type,
                            List<OmsOrderReturnItemOms> returnItemOmsList) {

        OmsOrderReturnItemOms returnItemOms = new OmsOrderReturnItemOms();

        BeanUtils.copyProperties(item, returnItemOms);
        returnItemOms.setReturnApplyOmsId(returnApplyOmsId);
        returnItemOms.setPrice(returnItemOms.getBoxPrice().divide(new BigDecimal( returnItemOms.getProductSkuSpec()),5, RoundingMode.UP));
        //根据类型情况另外一中的数量，并把当前类型的数量赋值给minUnitQuantity
        if ("useLess".equals(type)) {
            returnItemOms.setReuseUnitQuantity(0);
            returnItemOms.setUselessUnitQuantity(item.getUselessUnitQuantity());
            returnItemOms.setMinUnitQuantity(item.getUselessUnitQuantity());
        } else {
            returnItemOms.setUselessUnitQuantity(0);
            returnItemOms.setReuseUnitQuantity(item.getReuseUnitQuantity());
            returnItemOms.setMinUnitQuantity(item.getReuseUnitQuantity());
        }
        returnItemOms.setProductCount(returnItemOms.getMinUnitQuantity());
        returnItemOms.setTotalAmount(new BigDecimal(returnItemOms.getMinUnitQuantity()).multiply(returnItemOms.getPrice()));
        returnItemOms.setId(IdGenerator16Bit.getInstance().nextId());
        returnItemOmsList.add(returnItemOms);
    }
}
